package com.hwl.lab.controller;

import com.hwl.lab.entity.Chronic;
import com.hwl.lab.service.ChronicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@CrossOrigin //允许跨域请求
@RestController
public class ChronicController {

    @Autowired
    private ChronicService chronicService;

    //查询所有（分页）
    @GetMapping("/findChronicAll/{page}/{size}")
    public Page<Chronic> findChronicAll(@PathVariable("page") Integer page, @PathVariable("size") Integer size){
        return chronicService.findAll(page,size);
    }
    //查询所有不分页
    @GetMapping("/findChronicAll")
    public List<Chronic> findAll(){
        return chronicService.findAll();
    }

    @GetMapping("/findByChronicidF/{chronicid}")
    public List<Chronic> findByUserid(@PathVariable("chronicid") String chronicid){
        if (chronicService.findByChronicid(chronicid) != null) {
            List<Chronic> chronics = new ArrayList<>();
            chronics.add(chronicService.findByChronicid(chronicid));
            return chronics;
        } else {
            return null;
        }
    }
    @GetMapping("/findByChengidF/{chengid}")
    public List<Chronic> findByChengidid(@PathVariable("chengid") String chengid){
        if (chronicService.findByChengid(chengid) != null) {
            List<Chronic> chronics = new ArrayList<>();
            chronics.add(chronicService.findByChengid(chengid));
            return chronics;
        } else {
            return null;
        }
    }
    //添加，修改
    @PostMapping("/saveChronic")
    public void saveChronic(@RequestBody(required=false) Chronic chronic){
        Chronic chronic1 = chronic;
        chronic1.setMoney(0);
        chronicService.saveChronic(chronic1);
    }

    //通过账号删除
    @PostMapping("/deleteChronic/{id}")
    public void deleteChronic(@PathVariable("id") Long id){
        chronicService.deleteById(id);
    }
}
